import { createRouter, createWebHashHistory } from 'vue-router';
import AppLayout from '@/layout/AppLayout.vue';
import { usewwshStore } from '@/stores/wwsh';
import easyhpc from '../../easyhpc.json';

import { storeToRefs } from 'pinia';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name: 'ehpcdashboard',
            component: AppLayout,
            children: [
                {
                    path: '/',
                    name: 'ehpcdashboard',
                    component: () => import('@/views/ehpcDashboard.vue'),
                    children: [
                        {
                            path: '/',
                            component: () => import('@/views/ehpcTabViews/ehpcOverView.vue')
                        },
                        {
                            path: '/ehpcTabViews',
                            component: () => import('@/views/ehpcTabViews/ehpcOverView.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcOverView',
                            component: () => import('@/views/ehpcTabViews/ehpcOverView.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcSettings',
                            component: () => import('@/views/ehpcTabViews/ehpcSettings.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcLicenseInfo',
                            component: () => import('@/views/ehpcTabViews/ehpcLicenseInfo.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcVersionInfo',
                            component: () => import('@/views/ehpcTabViews/ehpcVersionInfo.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcRunCommand',
                            component: () => import('@/views/ehpcTabViews/ehpcRunCommand.vue')
                        },
                        {
                            path: '/ehpcTabViews/ehpcRackView',
                            component: () => import('@/views/ehpcTabViews/ehpcRackView.vue')
                        }
                    ]
                },
                {
                    path: '/ehpcInstall',
                    name: 'ehpcInstall',
                    component: () => import('@/views/ehpcInstall.vue'),
                    children: [
                        {
                            path: '/ehpcInstall',
                            component: () => import('@/views/pages/steps/stepDemo1.vue')
                        },
                        {
                            path: '/steps/stepDemo1',
                            component: () => import('@/views/pages/steps/stepDemo1.vue')
                        },
                        {
                            path: '/steps/stepDemo2',
                            component: () => import('@/views/pages/steps/stepDemo2.vue')
                        },
                        {
                            path: '/steps/stepDemo3',
                            component: () => import('@/views/pages/steps/stepDemo3.vue')
                        },
                        {
                            path: '/steps/stepDemo4',
                            component: () => import('@/views/pages/steps/stepDemo4.vue')
                        }
                    ]
                },
                {
                    path: '/accounting',
                    name: 'accounting',
                    component: () => import('@/views/ehpcTopViews/ehpcAccounting.vue')
                },
                {
                    path: '/setting',
                    name: 'setting',
                    component: () => import('@/views/ehpcTopViews/ehpcSetting.vue')
                }
            ]
        },
        {
            path: '/monitoring',
            name: 'monitoring',
            component: AppLayout,
            children: [
                {
                    path: '/monitoring',
                    name: 'monitoring',
                    component: () => import('@/views/ehpcTopViews/ehpcMonitoring.vue')
                }
            ]
        },
        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/access',
            name: 'access',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ]
});

// 前置守卫 路由拦截
router.beforeEach((to, from, next) => {
    if (to.path !== '/') {
        let store = usewwshStore();
        store.SetwwshData(easyhpc);
        let { WwshData } = storeToRefs(store);
        // 判断是否有路由
        if (hasRoute(to)) {
            return next();
        } else {
            // 添加路由
            creatRouteAsync(WwshData.value);
            return next(to.path);
        }
    } else {
        return next();
    }
});

creatRouteAsync(easyhpc);

function creatRouteAsync(data) {
    // 动态路由
    data.forEach((item) => {
        const routeConfig = {
            path: '/' + item.name,
            name: item.name,
            component: () => import(`../components/TableComponent.vue`)
            // component: () => import(`@/views/uikit/${item.name}.vue`)
        };
        router.addRoute('ehpcdashboard', routeConfig);
    });
}

function hasRoute(to) {
    const boo = router.getRoutes().some((item) => {
        return item.name === to.name;
    });
    return boo;
}

export default router;
